﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using SISLOG.BusinessLogic;
using SISLOG.BusinessEntities;

namespace SISLOG.Web
{
    public partial class frmCRUDMaterial :BasePage<frmCRUDMaterial> //System.Web.UI.Page
    {
        [Dependency]
        public IMaterialBL MaterialBL { get; set; }
        [Dependency]
        public IProveedorBL ProveedorBL { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ObjectDataSourceMaterial_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = MaterialBL;
        }

        protected void ObjectDataSourceProveedores_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = ProveedorBL;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Editar") {
                GridView1.SelectRow(int.Parse(e.CommandArgument.ToString()));  
                Material tempM = new Material();
                tempM = MaterialBL.GetAllFromMaterialByIdMaterial(int.Parse(GridView1.SelectedValue.ToString()));
                txtIdMaterial.Text = tempM.idMaterial.ToString();
                txtDescripcion.Text = tempM.descripcion;
                ddlProveedor.SelectedValue = tempM.idProveedor.ToString();
                txtFechaIngreso.Text = tempM.fechaIngreso.Year + "/" + tempM.fechaIngreso.Month + "/" +tempM.fechaIngreso.Day;
                txtStockIni.Text = tempM.stockInicial.ToString();
                txtStockFin.Text = tempM.stockFinal.ToString();
                txtPuntoReord.Text = tempM.puntoReorden.ToString();
                

                btnNuevoMaterial_ModalPopupExtender.Show();
            }
        }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            if (txtIdMaterial.Text == "0")
            { 
                txtIdMaterial.Visible = false;
                lblId.Visible = false; 
            }
            Material ObjMaterial = new Material();
            ObjMaterial.idMaterial = int.Parse(txtIdMaterial.Text);
            ObjMaterial.descripcion = txtDescripcion.Text;
            ObjMaterial.idProveedor = int.Parse(ddlProveedor.SelectedValue);
            ObjMaterial.fechaIngreso = DateTime.Parse(DateTime.Parse(txtFechaIngreso.Text).ToShortDateString());
            ObjMaterial.stockInicial = int.Parse(txtStockIni.Text);
            ObjMaterial.stockFinal = int.Parse(txtStockFin.Text);
            ObjMaterial.puntoReorden = int.Parse(txtPuntoReord.Text);

            MaterialBL.RegistrarMaterial(ObjMaterial);
            GridView1.DataBind(); // refrescar la grilla
            Response.Redirect("frmCRUDMaterial.aspx");

        }



    }
}